package com.wangzha.business.ctrl;

import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Record;
import com.wangzha.business.service.DeviceActiveCodeService;
import com.wangzha.business.service.DeviceGroupService;
import com.wangzha.common.base.BaseController;
import com.wangzha.common.model.DeviceActiveCode;
import com.wangzha.common.model.SysUser;
import com.wangzha.common.vo.Grid;
import com.wangzha.portal.core.service.SysUserService;
import java.util.Date;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;

@Path("/device/activecode")
public class DeviceActiveCodeController extends BaseController {
    @Inject
    DeviceActiveCodeService deviceActiveCodeService;

    @Inject
    DeviceGroupService deviceGroupService;

    @Inject
    SysUserService sysUserService;

    public void index() {
        setAttr("deviceGroupList", this.deviceGroupService.findByUserCode(getVisitor().getCode()));
        render("index.html");
    }

    public void list() {
        Record record = new Record();
        record.set("user_code", getVisitor().getCode());
        record.set("active_code", getPara("activeCode"));
        record.set("group_id", getPara("groupId"));
        Grid grid = this.deviceActiveCodeService.queryForListEq(getParaToInt("pageNumber", Integer.valueOf(1)).intValue(), getParaToInt("pageSize", Integer.valueOf(10)).intValue(), record);
        if (grid != null && CollectionUtils.isNotEmpty(grid.getList())) {
            List<Record> deviceList = (List<Record>)grid.getList();
            for (Record device : deviceList)
                device.set("group_name", this.deviceGroupService.findById(device.getStr("group_id")).getStr("name"));
            grid.setList(deviceList);
        }
        renderJson(grid);
    }

    public void add() {
        setAttr("deviceGroupList", this.deviceGroupService.findByUserCode(getVisitor().getCode()));
        render("add.html");
    }

    public void save() {
        SysUser sysUser = this.sysUserService.findByUserCode(getVisitor().getCode());
        if (sysUser != null && sysUser.getPort().intValue() > 0) {
            DeviceActiveCode entity = (DeviceActiveCode)getBean(DeviceActiveCode.class);
            entity.setCreateTime(new Date());
            entity.setUserCode(getVisitor().getCode());
            if (entity.save()) {
                sysUser.setPort(Integer.valueOf(sysUser.getPort().intValue() - 1));
                sysUser.update();
                setAttr("deviceActiveCode", entity);
            } else {
                setException("激活码添加失败");
            }
        } else {
            setException("端口不足");
        }
        render("add.html");
    }

    public void edit() {
        setAttr("deviceActiveCode", this.deviceActiveCodeService.findById(getPara(0)));
        setAttr("deviceGroupList", this.deviceGroupService.findByUserCode(getVisitor().getCode()));
        render("edit.html");
    }

    public void update() {
        DeviceActiveCode entity = (DeviceActiveCode)getBean(DeviceActiveCode.class);
        entity.update();
        setAttr("deviceActiveCode", entity);
        render("edit.html");
    }

    public void delete() {
        this.deviceActiveCodeService.deleteByIds(getIds());
        SysUser sysUser = this.sysUserService.findByUserCode(getVisitor().getCode());
        sysUser.setPort(Integer.valueOf(sysUser.getPort().intValue() + getIds().size()));
        sysUser.update();
        renderJson(suc());
    }
}
